<?php
namespace home\Controller;

use Think\Controller;

class UserController extends ApibaseController
{
    function __construct()
    {
        parent::__construct();
    }

    /*
     * 登陆模块
     */
    public function login()
    {
        global $isLogin;
        if ($isLogin) {
            redirect('/drug/index?res=0-1');
        } else {
            $this->display('user/login');
        }
    }


    /*
     * 系统登陆
     */
    public function checklogin()
    {
        $userMsg = M('ResEntity')->where(['username' => I('post.username')])->find();
        if(!$userMsg){
            jsonRespons('0', '帐号不存在');
        }
        if(!$userMsg['is_active']){
            jsonRespons('0', '帐号已被禁止登录，请联系客服！');
        }
        if ($userMsg['password'] == md5(I('post.passwd') . $userMsg['salt'])) {
            $pushSession = ['name' => I('post.username')];
            if (I('post.signlogin') == 1) {
                $setSecret = md5(I('post.username') . 'ajwUid' . time());
                M('ResEntity')->where(['username' => I('post.username')])->setField(
                    ['remember_token' => $setSecret]
                );

                cookie('_ajwunion', $setSecret, 'expire=604800');
                $pushSession['expire'] = 604800;
            }
            $role_id = M('AuthGroupAccess')->where(['uid' => $userMsg['rid']])->getField('role_id');
            session($pushSession);
            session('role_id', $role_id);
            session('loginUid', $userMsg['rid']);
            session('loginUname', trim(I('post.username')));
            cookie('ajwUid', base64_encode(I('post.username')) . randstr(), 'expire=604800');

            session(I('post.username'), true);
            // echo 2332;exit;
            putlog('登陆系统', 'UserController', 'checklogin');
            jsonRespons('1', '登陆成功！');
        } else {
            jsonRespons('0', '帐号或密码有误！');
        }
    }


    /*
     * 系统注销操作
     */
    public function loginout()
    {
        $userMsg = getLoginMsg();
        session('role_id', null);
        session('loginUid', null);
        session(cookie('PHPSESSID'), null);
        session(getLoginMsg()->username, null);
        session('role_id', null);
        cookie('ajwUid', null);
        cookie('_ajwunion', null);
        cookie('PHPSESSID', null);
        redirect('/user/login');
    }

    /*
     * 个人中心简介
     * @date: 2015-3-23下午2:30:20
     * @editor: XING
     */
    public function usercenter()
    {
        //个人页面渲染
        $this->display('user/usercenter');
    }

    /*
     * 个人中心资料编辑
     */
    public function userUpdate()
    {
        if (I('post.update')) {
            $data = ['email' => I('post.email')];
            if (I('post.password')) {
                $salt             = randstr(5);
                $data['password'] = md5(I('post.password') . $salt);
                $data['salt']     = $salt;
            }
            $data['email'] = I('post.email');
            session('kfLine' . I('post.rid'), I('post.lineStatus') == '1' ? '1' : null);
            $keArr = session('kfLineArr');
            if (I('post.lineStatus') == '1') {
                if (isset($keArr[ I('post.rid') ]) && $keArr[ I('post.rid') ] = I('post.rid')) {
                    // Nothing to do
                } else {
                    $keArr[ I('post.rid') ] = I('post.rid');
                }
                session('kfLineArr', $keArr);
            } else {
                if (isset($keArr[ I('post.rid') ])) {
                    unset($keArr[ I('post.rid') ]);
                    session('kfLineArr', $keArr);
                }
            }
            M('ResEntity')->where(['rid' => session('loginUid')])->setField($data);

            $this->showtips('信息编辑成功');
        }
    }

    /*
     * 后台管理员管理
     */
    public function admin()
    {
        if (!is_login()) {
            redirect('/user/login');
            exit;
        }
        $admin = M('ResEntity e')
            ->field('e.rid,e.username,r.name,e.email,e.add_at,e.is_active')
            ->join('ymr_auth_group_access a on a.uid=e.rid')
            ->join('ymr_role r on r.id=a.role_id')
            ->select();
        $this->assign('getData', $admin);
        $this->display('user/admin');
    }

    /*
     * 冻结&解封用户
     */
    public function decate()
    {
        if (I('get.types')) {
            M('ResEntity')->where(['rid' => I('get.custid')])->setField(['is_active' => '1']);
            $this->showtips('成功解封该用户');
        }
        M('ResEntity')->where(['rid' => I('get.custid')])->setField(['is_active' => '0']);
        $this->showtips('成功冻结该用户');
    }

    /*
     * 后台管理员管理
     */
    public function addAdmin()
    {
        if (!is_login()) {
            redirect('/user/login');
            exit;
        }
        /*用户注册逻辑*/
        if (I('post.submit')) {
            if (I('post.rid')) {
                $data = ['email' => I('post.email')];
                if (I('post.password')) {
                    $salt             = randstr(5);
                    $data['password'] = md5(I('post.password') . $salt);
                    $data['salt']     = $salt;
                }
                $data['username'] = I('post.username');
                if (I('post.role_id')) {
                    M('AuthGroupAccess')->where(['uid' => I('post.rid')])->setField(['role_id' => I('post.role_id')]);
                    $data['role_id'] = I('post.role_id');
                }
                M('ResEntity')->where(['rid' => I('post.rid')])->setField($data);
                putlog('修改管理员' . $data['username'] . '的信息', 'UserController', 'addAdmin');
                $this->showtips('信息编辑成功');
            }
            $salts = randstr(5);
            $data  = [
                'username'       => I('post.username'),
                'password'       => md5(I('post.password') . $salts),
                'salt'           => $salts,
                'email'          => I('post.email'),
                'role_id'        => I('post.role_id'),
                'remember_token' => '',
                'add_at'         => time(),
                'update_at'      => time()
            ];
            $res   = M('ResEntity')->add($data);
            if (I('post.role_id')) {
                M('AuthGroupAccess')->add(['uid' => $res, 'role_id' => I('post.role_id')]);
            }
            putlog('添加管理员' . I('post.username'), 'UserController', 'addAdmin');
            if ($res) {
                $this->showtips('添加管理员成功');
            } else {
                $this->showtips('操作失败');
            }
        }
        $admin_info = M('resEntity e')
            ->join('ymr_auth_group_access a on a.uid=e.rid')
            ->join('ymr_role r on r.id=a.role_id')
            ->where(['e.rid' => I('get.rid')])
            ->find();
        $this->assign('admin_info', $admin_info);
        $role = M('role')->where(['status' => 1])->select();
        $this->assign('role', $role);
        //注册页面渲染
        $this->display('user/addadmin');
    }
}